<template>
  <div>
    <van-nav-bar fixed placeholder title="文章详情" left-arrow @click-left="$router.back()"/>
    <h2 class="title">{{ detail.title }}</h2>
    <!-- 作者信息 -->
    <div class="aut-box">
      <van-image class="avatar" round fit="cover" :src="detail.aut_photo" />
      <div class="info-box">
        <div>{{ detail.aut_name }}</div>
        <span>{{ relvTime(detail.pubdate) }}</span>
      </div>
      <van-button round type="info" size="small">+ 关注</van-button>
    </div>

    <!-- 内容区域 -->
    <div class="content" v-html="detail.content"></div>

    <MyComment />
  </div>
</template>

<script>
import { detailAPI } from '@/api/article'
// 导入dayjs
import dayjs from 'dayjs'
// 导入相对时间插件
import relativeTime from 'dayjs/plugin/relativeTime'
// 导入中文语言包
import 'dayjs/locale/zh-cn'

// 导入组件
import MyComment from './MyComment.vue'
// 让dayjs运用插件
dayjs.extend(relativeTime)
dayjs.locale('zh-cn')
export default {
  name: 'MyArticle',
  components: {
    MyComment
  },
  data () {
    return {
      detail: {}
    }
  },
  methods: {
    // 自己封装的转换相对的时间的方法
    relvTime (time) {
      return dayjs(time).fromNow()
    }
  },
  async created () {
    const res = await detailAPI(this.$route.params.id)
    this.detail = res.data
  }
}
</script>

<style lang="scss" scoped>
.title {
  font-size: 18px;
  margin: 15px;
}
.content {
  margin: 15px;
  font-size: 16px;

  ::v-deep {
    img {
      width: 100%;
    }

    pre {
      background-color: #000;
      color: lightgray;
      overflow: auto;
    }
  }
}

.aut-box {
  display: flex;
  align-items: center;
  margin: 15px;
  .avatar {
    width: 80px;
    height: 80px;
  }
  .info-box {
    flex: 1;
    margin-left: 10px;
    div {
      margin-bottom: 10px;
    }
    span {
      color: gray;
      font-size: 12px;
    }
  }
}
</style>
